<!--外购一般固体废物利用处置年度台账-->
<template>
    <Layout>
        <Form ref="form1" :model="dataToQuery" :label-width="120">
            <Row class="query-form">
                <Col span="8">
                    <FormItem label="单位名称：" prop="order">
                        <cascader-dom class="query-form-el" ref='queryOrgCascader' @setOrgInfo='setQueryCompanyName'
                                      :orgTree="true"
                                      :setCurrentName="{label:dataToQuery.companyName,value:dataToQuery.companyId}"></cascader-dom>
                    </FormItem>
                </Col>
                <Col span="8">
                    <FormItem label="名称：" >
                        <Input class='query-form-el' placeholder="请填写名称" type='text' v-model='dataToQuery.name'></Input>
                    </FormItem>
                </Col>
                <Col span="8" v-if="!switchFlag">
                    <Button @click="switchQuery(true)" class="ivu-btn-switch" type="text">展开∨</Button>
                    <Button @click="reset" class="ivu-btn-reset">重置</Button>
                    <Button type="primary" class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
                </Col>
                <Col span="8" v-else>
                    <FormItem label='接收时间：'>
                        <DatePicker class="query-form-el" v-model="dataToQuery.receivingTimeRange" format="yyyy-MM-dd" type="daterange" placement="bottom-end" placeholder="请选择查询时间段"
                                  :clearable="false" @on-change="(date) => onSelectFieldQueryDateRange(date, 'receivingTime')"></DatePicker>
                    </FormItem>
                </Col>
            </Row>
            <Row v-show="switchFlag" class="query-form">

                <Col span="8" offset="16">
                    <Button @click="switchQuery(false)" class="ivu-btn-switch" type="text">收起∧</Button>
                    <Button @click="reset" class="ivu-btn-reset">重置</Button>
                    <Button class="ivu-btn-query" icon="md-search" @click="query">查询</Button>
                </Col>
            </Row>
            <Row class="query-form">
                <Col span="24">
                    <Button size="large" class="ivu-btn-add" icon="md-add" @click="addInfo">新增</Button>
                    <download-button :api="exportUrl" :queryForm="dataToQuery" />
                    <!-- <Button type="primary" class="ivu-btn-add" icon="md-cloud-download" @click="exportFormSubmit">导出</Button> -->
                </Col>
            </Row>
        </Form>
        <Table ref='table' :height="tableHeight" no-data-text="没有查询到相关数据!" @on-selection-change='getSelectRows'
               :loading='pageStatus.loading'
               border :columns="columns1" :data="dataTable">
        </Table>
        <Row style="margin-right:10px;">
            <Col span="24">
                <Page style="float: right" :total="pagedata.total" :page-size="pagedata.pageSize"
                      @on-change="pageChange" @on-page-size-change="pageSizeChange" :page-size-opts="pageSizeArray"
                      show-total show-sizer show-elevator/>
            </Col>
        </Row>
        <Modal :title="this.getModalTitle('外购一般固体废物利用处置年度台账')" :transfer=false :mask-closable="false" v-model="dialogShow"
               v-if="dataToView && dialogShow" :width=800 class-name='limit-height-dialog'>
            <Row>
                <Col span="24">
                    <Form ref="dialogForm" :class="mode === 'view'?'modal-view-detail':''" :show-message="false"
                          :model="dataToView" :rules="mode === 'view'?{}:ruleCustom"
                          :label-width="mode === 'view'?160:null" :label-position="mode === 'view'?'right':'top'">
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='所属二级集团：' prop='belongOrgId'>
                                    <div v-if="mode === 'view'">{{ dataToView.belongOrgName }}</div>
                                    <!--<Input v-else class='query-form-el' placeholder="请填写所属二级集团" type='text' v-model='dataToView.belongOrgId'></Input>-->
                                    <Select v-else class='query-form-el' placeholder="集团名称"  v-model='dataToView.belongOrgId' label-in-value @on-change="belongChange">
                                        <Option v-for="item in twoLevel" :value="item.orgId" :key="item.orgId">{{item.orgName}}</Option>
                                    </Select>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='单位名称：' prop='companyId'>
                                    <div v-if="mode === 'view'">{{ dataToView.companyName }}</div>
                                    <!--<Input v-else class='query-form-el' placeholder="请填写单位名称" type='text' v-model='dataToView.companyId'></Input>-->
                                    <div v-else>
                                        <cascader-dom  v-if="show"  class='query-form-el' ref='orgCascader' @setOrgInfo='setCompanyId' @setOrgname="setCompanyName" :orgTree="true"
                                                       :currentOrgId="dataToView.companyId" :rootOrgId="dataToView.belongOrgId"
                                                       :setCurrentName="{label:dataToView.companyName,value:dataToView.companyId}" ></cascader-dom>
                                    </div>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='名称：' prop='name'>
                                    <div v-if="mode === 'view'">{{ dataToView.name }}</div>
                                    <Input v-else class='query-form-el' placeholder="请填写名称" type='text' v-model='dataToView.name'></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='类别：' prop='category'>
                                    <div v-if="mode === 'view'">{{ dataToView.categoryName }}</div>
                                    <!--<Input v-else class='query-form-el' placeholder="请填写类别" type='text' v-model='dataToView.category'></Input>-->
                                    <Select v-else v-model='dataToView.category' placeholder="请填写类别" label-in-value class='query-form-el'>
                                        <Option v-for="item in dictMap.categorys" :value="item.codingCodeId" :key="item.codingCodeId">{{item.codingName }}
                                        </Option>
                                    </Select>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='类别代码：' prop='categoryCode'>
                                    <div v-if="mode === 'view'">{{ dataToView.categoryCode }}</div>
                                    <Input v-else class='query-form-el' placeholder="请填写类别代码" type='text' v-model='dataToView.categoryCode'></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='期初贮存量（t）：' prop='openingStockNum'>
                                    <div v-if="mode === 'view'">{{ dataToView.openingStockNum }}</div>
                                    <InputNumber v-else class='query-form-el'placeholder="请填写期初贮存量（t）" min="0" type='text' v-model='dataToView.openingStockNum'></InputNumber>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='接收量（t）：' prop='receivingNum'>
                                    <div v-if="mode === 'view'">{{ dataToView.receivingNum }}</div>
                                    <InputNumber v-else class='query-form-el'placeholder="请填写接收量（t）" min="0" type='text' v-model='dataToView.receivingNum'></InputNumber>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='接收时间：' prop='receivingTime'>
                                    <div v-if="mode === 'view'">{{ renderViewDate(dataToView.receivingTime) }}</div>
                                    <DatePicker v-else class="query-form-el" placeholder="请选择接收时间" v-model='dataToView.receivingTime' ></DatePicker>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='利用处置量（t）：' prop='disposalNum'>
                                    <div v-if="mode === 'view'">{{ dataToView.disposalNum }}</div>
                                    <InputNumber v-else class='query-form-el' placeholder="请填写利用处置量（t）" min="0" type='text' v-model='dataToView.disposalNum'></InputNumber>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='利用处置方式：' prop='disposalType'>
                                    <div v-if="mode === 'view'">{{ dataToView.disposalType }}</div>
                                    <Input v-else class='query-form-el' placeholder="请填写利用处置方式" type='text' v-model='dataToView.disposalType'></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='期末贮存量（t）：' prop='endingStockNum'>
                                    <div v-if="mode === 'view'">{{ dataToView.endingStockNum }}</div>
                                    <InputNumber v-else class='query-form-el'placeholder="请填写期末贮存量（t）" min="0" type='text' v-model='dataToView.endingStockNum'></InputNumber>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='产生单位：' prop='generatingUnit'>
                                    <div v-if="mode === 'view'">{{ dataToView.generatingUnit }}</div>
                                    <Input v-else class='query-form-el' placeholder="请填写产生单位" type='text' v-model='dataToView.generatingUnit'></Input>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around"
                             class="code-row-bg">
                            <Col span="23">
                                <FormItem v-if="mode === 'view'" label="来源证明：">
                                    <div v-show="dataToView.idFiles.length>0"
                                         v-for="(item,index) in dataToView.idFiles">
                                        <a @click="downloadFiles(item,index)"><span
                                                style="color: #3399ff;margin-left: 5px">{{ item.fileName }}</span></a>
                                        <Progress v-show="uploadListVisible[index]"
                                                  :percent="uploadListPercent[index]"></Progress>
                                    </div>
                                </FormItem>
                                <FormItem v-else label="来源证明：" prop="idFiles">
                                    <div>
                                        <Upload ref="upload"
                                                :before-upload="(file) => { beforeUpload1(file, 'id', 'purchasedSolidWaste') }"
                                                :show-upload-list="true"
                                                :max-size="10240"
                                                multiple
                                                type="drag"
                                                :action="fileUrl">
                                            <div style="padding: 10px">
                                                <Icon type="ios-cloud-upload" size="45" style="color: #3399ff"></Icon>
                                                <p>点击将文件拖拽到这里上传</p>
                                                <!--<span style="font-size:8px;color: #797a64">支持扩展名：.rar .zip .doc .docx .pdf...</span>-->
                                            </div>
                                        </Upload>
                                    </div>
                                    <div v-show="dataToView.idFiles.length>0" v-for="(item,index) in dataToView.idFiles"
                                         :key='item'>
                                        <a @click="downloadFiles(item,index)">{{ item.fileName }}</a>
                                        <Button type="text" icon="md-trash" @click="removeFile1(item.id, 'id', 'purchasedSolidWaste')"
                                                style="margin-left: 5px;margin-bottom: 3px"/>
                                        <Progress v-show="uploadListVisible[index]"
                                                  :percent="uploadListPercent[index]"></Progress>
                                        <!--<Progress  v-show="filePercentVisible[index]" :percent="filePercent[index]"></Progress>-->
                                    </div>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='填报人：' prop='createUserName'>
                                    <div v-if="mode === 'view'">{{ dataToView.createUserName }}</div>
                                    <Input v-else class='query-form-el' placeholder="请填写填报人" type='text' readonly
                                           v-model='dataToView.createUserName'></Input>
                                </FormItem>
                            </Col>
                            <Col span="11">
                                <FormItem label='填报时间：' prop='createDate'>
                                    <div v-if="mode === 'view'">{{ renderViewDate(dataToView.createDate) }}</div>
                                    <DatePicker v-else class="query-form-el" placeholder="请选择填报时间" readonly
                                                v-model='dataToView.createDate'></DatePicker>
                                </FormItem>
                            </Col>
                        </Row>
                        <Row type="flex" justify="space-around" class="code-row-bg">
                            <Col span="11">
                                <FormItem label='填报人手机号：' prop='registrantPhone'>
                                    <div v-if="mode === 'view'">{{ dataToView.registrantPhone }}</div>
                                    <Input v-else class='query-form-el' placeholder="请填写填报人手机号" type='text'
                                           v-model='dataToView.registrantPhone'></Input>
                                </FormItem>
                            </Col>
                            <Col span="11"></Col>
                        </Row>

                    </Form>
                </Col>
            </Row>
            <div slot="footer">
                <Button v-if="mode != 'view'" type="primary" class='ivu-btn-save' @click="submit('dialogForm')">确定
                </Button>
                <Button v-if="mode != 'view'" type="primary" class='ivu-btn-cancel' @click="handleCancel">取消</Button>
                <Button v-if="mode === 'view'" class='ivu-btn-save' @click="dialogShow=false">关闭</Button>
            </div>
        </Modal>

    </Layout>
</template>

<script>
    import {Api} from '@/api/index.js'
    import {formatDate} from '@/tools/index'
    var cascaderDom = () => import('@/components/cascader-dom.vue');
    import pageCommonFunction from '@/components/pageCommonFunction.js'
    import {sessionStorage, cookie} from '@/common/cacheUtils.js';

    export default {
        components: {cascaderDom},
        data() {
            return {
                ...this.createCommonDataField(),
                imgUrl: "",
                columns1: [
                    {title: '序号', width: 70, fixed: 'left', align: 'center', render: this.indexColumnsRenderSum},
                    { key:'belongOrgName',title: '所属二级集团',  align: 'center', minWidth: 120, },
                    { key:'companyName',title: '单位名称',  align: 'center', minWidth: 120, },
                    { key:'name',title: '名称',  align: 'center', minWidth: 120, },
                    { key:'categoryName',title: '类别',  align: 'center', minWidth: 120, },
                    { key:'categoryCode',title: '类别代码',  align: 'center', minWidth: 120, },
                    { key:'openingStockNum',title: '期初贮存量（t）',  align: 'center', minWidth: 120, },
                    { key:'receivingNum',title: '接收量（t）',  align: 'center', minWidth: 120, },
                    { key:'receivingTime',title: '接收时间',  align: 'center', minWidth: 120,render: this.renderCellDate },
                    { key:'disposalNum',title: '利用处置量（t）',  align: 'center', minWidth: 120, },
                    { key:'disposalType',title: '利用处置方式',  align: 'center', minWidth: 120, },
                    { key:'endingStockNum',title: '期末贮存量（t）',  align: 'center', minWidth: 120, },
                    { key:'generatingUnit',title: '产生单位',  align: 'center', minWidth: 120, },

                    // { key:'createUserName',title: '填报人',  align: 'center',minWidth: 120, },
                    {key: 'createDate', title: '填报时间', align: 'center', minWidth: 120,render: this.renderCellDate},
                    // { key:'registrantPhone',title: '填报人手机号',  align: 'center',minWidth: 120, },
                    this.getActionColumns((params) => {return params.index === 0;}),
                ],
                ruleCustom: {
                    belongOrgId: [ { required: true, message: '请填写所属二级集团', trigger: 'change',}],
                    companyId: [ { required: true, message: '请填写单位名称', trigger: 'change',}],
                    name: [ { required: true, message: '请填写名称', trigger: 'change',}],
                    category: [ { required: true, message: '请填写类别', trigger: 'change',}],
                    // categoryCode: [ { required: true, message: '请填写类别代码', trigger: 'change',}],
                    openingStockNum: [ { required: true, message: '请填写期初贮存量（t）', trigger: 'change',type:'number'}],
                    receivingNum: [ { required: true, message: '请填写接收量（t）', trigger: 'change',type:'number'}],
                    receivingTime: [ { required: true, message: '请填写接收时间', trigger: 'change',type:'date'}],
                    disposalNum: [ { required: true, message: '请填写利用处置量（t）', trigger: 'change',type:'number'}],
                    disposalType: [ { required: true, message: '请填写利用处置方式', trigger: 'change',}],
                    endingStockNum: [ { required: true, message: '请填写期末贮存量（t）', trigger: 'change',type:'number'}],
                    generatingUnit: [ { required: true, message: '请填写产生单位', trigger: 'change',}],
                    createUserName: [{required: true, message: '请填写填报人', trigger: 'change',}],
                    createDate: [{required: true, message: '请填写填报时间', trigger: 'change', type: 'date'}],
                    registrantPhone: [{
                        validator: (rule, value, callback) => {
                            if (!(/^1[3456789]\d{9}$/.test(this.dataToView.registrantPhone))) {
                                callback(' ');
                                // this.$Message.error("请填写有效的的手机号")
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'blur'
                    }],
                    idFiles: [{
                        validator: (rule, value, callback) => {
                            if (this.dataToView.idFiles.length < 1) {
                                callback('附件必须上传')
                            } else {
                                callback()
                            }
                        },
                        required: true,
                        trigger: 'change'
                    }]
                },
                tableHeight: 10,
                dictMap: {},
                punishment: false,
                penaltyAmount: true,
                // api
                queryUrl: Api.queryEnTable.replace('{{table}}', 'PurchasedSolidWaste'),
                exportUrl: Api.exportEnTable.replace('{{table}}', 'PurchasedSolidWaste'),
                deleteUrl: Api.deleteEnTable.replace('{{table}}', 'PurchasedSolidWaste'),
                getUrl: Api.getEnTable.replace('{{table}}', 'PurchasedSolidWaste'),
                updateUrl: Api.updateEnTable.replace('{{table}}', 'PurchasedSolidWaste'),
                saveUrl: Api.saveEnTable.replace('{{table}}', 'PurchasedSolidWaste'),
                show:true,
                currentUser: JSON.parse(sessionStorage('userId').get('userId')),

            }
        },
        async mounted() {
            this.setTableHeight();
            this.selectSecGroupNames()
            this.query();
            let _this = this
            this.gDic(Api.queryCodeDetailBySortId + "/env-00015", function (result) {
                _this.dictMap.categorys = result;
            });
        },
        methods: {
            ...pageCommonFunction,
            selectSecGroupNames(){
                this.$http.get(Api.quqeryTwoOrgInfo).then(res => {
                    this.twoLevel=res.data.data
                }).catch(e => {
                })
            },
            belongChange(value) {
                this.$set(this.dataToView,'belongOrgId',value.value)
                this.$set(this.dataToView,'belongOrgName',value.label)
                this.dataToView.companyId = null
                this.dataToView.companyName = null
                this.$refs.orgCascader.resetCurrentOrg();
                this.show = false;
                setTimeout(() => {
                    this.show = true
                }, 0)
            },
            deleteData(params) {
                this.deleteDateAjax(params);
            },
            query() {
                // if(this.dataToQuery.eventTypeIdsArrays) {
                //     this.dataToQuery.eventTypeIds = this.arrayToInFormat(this.dataToQuery.eventTypeIdsArrays)
                // }
                // this.dataToQuery.orgId = this.currentUser.companyId;
                this.queryAjax();
            },

            submit(name) {
                this.submitAjax(name);
            },
            createEmptyDataToAdd() {
                return {
                    createDate: new Date(),
                    createUserId: this.currentUser.userId,
                    createUserName: this.currentUser.userName,
                    openingStockNum: null,
                    receivingNum: null,
                    disposalNum: null,
                    endingStockNum: null,
                    id: this.get_uuid(),
                    idFiles: [],
                }
            },
            async afterDataToViewInit() {
                await this.getFiles1('id', 'purchasedSolidWaste');
            },
        },
        watch: {},
    }
</script>

<style lang='css' scoped>
    .ivu-form-item {
        padding-right: 20px;
    }

    .query-form-el, .ivu-date-picker, .ivu-input-wrapper {
        width: 100%;
    }

</style>